<template>
    <van-address-edit :area-list="areaList" show-postal show-delete show-set-default show-search-result
        :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave"
        @delete="onDelete" @change-detail="onChangeDetail" :address-info="arr" />
</template>

<script>
import axios from 'axios';
let service = axios.create();
import { areaList } from '@vant/area-data';
export default {
    data() {
        return {
            areaList,
            searchResult: [],
            arr: {},              //页面初始值为arr
            id: ''
        };
    },
    methods: {
        onSave(value) {         //修改地址，全部修改，value为修改之后的数据
            if (value.isDefault) {
                service({
                    url: `http://localhost:3000/address`,           //发请求获取其他为默认地址的地址
                    method: 'get',
                    params: {
                        isDefault: true
                    }
                }).then(res => {
                    console.log(res.data)
                    if (res.data != '') {                     //如果有
                        this.id = res.data
                        console.log(this.id[0].id)
                        service({
                            url: `http://localhost:3000/address/${this.id[0].id}`,     //发请求修改他的默认为非默认
                            method: 'patch',
                            data: {
                                isDefault: false
                            }
                        }).then(res => {
                            console.log('chengg')
                            service({
                                url: `http://localhost:3000/address/${this.arr.id}`,    //然后修改所有内容
                                method: "put",
                                data: {
                                    name: value.name,
                                    tel: value.tel,
                                    country: value.country,
                                    province: value.province,
                                    city: value.city,
                                    county: value.county,
                                    areaCode: value.areaCode,
                                    postalCode: value.postalCode,
                                    addressDetail: value.addressDetail,
                                    isDefault: value.isDefault,
                                    userid: sessionStorage.getItem('userid'),
                                }
                            }).then(res => {
                                this.$router.push('/Myaddress')
                            });

                        })
                    } else {
                        service({
                            url: `http://localhost:3000/address/${this.arr.id}`,
                            method: "put",
                            data: {
                                name: value.name,
                                tel: value.tel,
                                country: value.country,
                                province: value.province,
                                city: value.city,
                                county: value.county,
                                areaCode: value.areaCode,
                                postalCode: value.postalCode,
                                addressDetail: value.addressDetail,
                                isDefault: value.isDefault,
                                userid: sessionStorage.getItem('userid'),
                            }
                        }).then(res => {
                            this.$router.push('/Myaddress')
                        });

                    }
                })




            } else {

                service({
                    url: `http://localhost:3000/address/${this.arr.id}`,
                    method: "put",
                    data: {
                        name: value.name,
                        tel: value.tel,
                        country: value.country,
                        province: value.province,
                        city: value.city,
                        county: value.county,
                        areaCode: value.areaCode,
                        postalCode: value.postalCode,
                        addressDetail: value.addressDetail,
                        isDefault: value.isDefault,
                        userid: sessionStorage.getItem('userid'),
                    }
                }).then(res => {
                    // console.log(res.arr);
                });
                this.$router.push('/Myaddress')
                this.$router.go(0)
            }                 //页面自动刷新
        },
        onDelete() {       //删除地址
            // console.log(111)
            service({
                url: `http://localhost:3000/address/${this.arr.id}`,
                method: "delete",
            }).then(res => {
                this.$router.push('/Myaddress')
            });

        },
        onChangeDetail(val) {
            if (val) {
                this.searchResult = [
                    {
                        name: '立人科技园',
                        address: '西安市雁塔区',
                    },
                ];
            } else {
                this.searchResult = [];
            }
        },
    },
    mounted() {
        // console.log(this.$route.query.txt)        接收传过来的参数，赋值给arr
        this.arr = this.$route.query.txt
    }
}
</script>

<style>

</style>